package com.peng.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.constant.Constants;
import com.peng.mapper.TClueMapper;
import com.peng.model.TClue;
import com.peng.model.TCustomer;
import com.peng.query.CustomerQuery;
import com.peng.result.CustomerExcel;
import com.peng.service.TCustomerService;
import com.peng.mapper.TCustomerMapper;
import com.peng.util.JWTUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Love-ovo
* @description 针对表【t_customer(客户表)】的数据库操作Service实现
* @createDate 2025-03-14 21:28:15
*/
@Service
public class TCustomerServiceImpl extends ServiceImpl<TCustomerMapper, TCustomer>
    implements TCustomerService{

    @Autowired
    private TCustomerMapper tCustomerMapper;

    @Autowired
    private TClueMapper tClueMapper;

    @Transactional(rollbackFor = Exception.class)
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        //1、验证该线索是否已经转过客户，转过了就不能再转了
        TClue tClue = tClueMapper.selectById(customerQuery.getClue_id());

        if (tClue.getState() == -1) {
            throw new RuntimeException("该线索已经转过客户，不能再转了.");
        }

        //2、向客户表插入一条数据
        TCustomer tCustomer = new TCustomer();
        //把CustomerQuery对象里面的属性数据复制到TCustomer对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(customerQuery, tCustomer);

        tCustomer.setCreate_time(new Date()); //创建时间
        //登录人的id 创建人
        tCustomer.setCreate_by(JWTUtils.parseUserFromJWT(customerQuery.getToken()).getId());
        int insert = tCustomerMapper.insert(tCustomer);

        //3、把线索表的数据状态改为-1（已转客户）
        TClue clue = new TClue();
        clue.setId(customerQuery.getClue_id());
        clue.setState(-1);
        int update = tClueMapper.updateById(clue);

        return insert >= 1 && update >= 1;
    }

    @Override
    public List<CustomerExcel> getCustomerByExcel(List<Integer> idList) {
        List<CustomerExcel> customerExcelList = new ArrayList<>();

        List<TCustomer> tCustomerList = tCustomerMapper.selectCustomerByExcel(idList);

        //把从数据库查询出来的List<TCustomer>数据，转换为 List<CustomerExcel>数据
        tCustomerList.forEach(tCustomer -> {
            CustomerExcel customerExcel = new CustomerExcel();

            //需要一个一个设置，没有办法，因为没法使用BeanUtils复制
            customerExcel.setOwner_name(ObjectUtils.isEmpty(tCustomer.getOwnerDO()) ? Constants.EMPTY : tCustomer.getOwnerDO().getName());
            customerExcel.setActivity_name(ObjectUtils.isEmpty(tCustomer.getActivityDO()) ? Constants.EMPTY : tCustomer.getActivityDO().getName());
            customerExcel.setFull_name(tCustomer.getClueDO().getFull_name());
            customerExcel.setAppellation_name(ObjectUtils.isEmpty(tCustomer.getAppellationDO()) ? Constants.EMPTY : tCustomer.getAppellationDO().getType_value());
            customerExcel.setPhone(tCustomer.getClueDO().getPhone());
            customerExcel.setWeixin(tCustomer.getClueDO().getWeixin());
            customerExcel.setQq(tCustomer.getClueDO().getQq());
            customerExcel.setEmail(tCustomer.getClueDO().getEmail());
            customerExcel.setAge(tCustomer.getClueDO().getAge());
            customerExcel.setJob(tCustomer.getClueDO().getJob());
            customerExcel.setYear_income(tCustomer.getClueDO().getYear_income());
            customerExcel.setAddress(tCustomer.getClueDO().getAddress());
            customerExcel.setNeed_load_name(ObjectUtils.isEmpty(tCustomer.getNeedLoanDO()) ? Constants.EMPTY : tCustomer.getNeedLoanDO().getType_value());
            customerExcel.setProduct_name(ObjectUtils.isEmpty(tCustomer.getIntentionProductDO()) ? Constants.EMPTY : tCustomer.getIntentionProductDO().getName());
            customerExcel.setSource_name(ObjectUtils.isEmpty(tCustomer.getSourceDO()) ? Constants.EMPTY : tCustomer.getSourceDO().getType_value());
            customerExcel.setDescription(tCustomer.getDescription());
            customerExcel.setNext_contact_time(tCustomer.getNext_contact_time());

            customerExcelList.add(customerExcel);
        });

        return customerExcelList;
    }
}




